import Vue from "vue";
import VueRouter from "vue-router";
import Login from "../components/Login.vue";
import Home from "../components/Home.vue";
import welcome from "../components/welcome.vue";
import No from "../components/No.vue";
import User from "../components/User/user.vue";

Vue.use(VueRouter);
Vue.use(Login);
Vue.use(Home);
Vue.use(welcome);
Vue.use(User);

const routes = [
  { path: "/login", component: Login },
  {
    path: "/home",
    component: Home,
    redirect: "/welcome",
    children: [
      { path: "/welcome", component: welcome },
      { path: "/users", component: User },
    ],
  },
  { path: "/", redirect: "/login" },
  { path: "*", component: No },
];

const router = new VueRouter({
  routes,
});

router.beforeEach((to, from, next) => {
  if (to.path === "/login") return next();
  const tokenstr = window.sessionStorage.getItem("token");
  if (!tokenstr) return next("/login");
  next();
});

export default router;
